<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/07/10
 * Time: 13:57
 */

class VolumeController extends BaseController{

    /**
     * @Author:      sqq
     * @DateTime:    2018-07-09 17:17:29
     * @Description: 访问量统计(C端所有统计访问量都用此接口)
     * @param:  XXX          [参数由前端自行定义，json格式传输]
     * {%20"branner_id":1,%20"target_url":"/activity/carInsurance",%20"source":1,%20"model":1,%20"type":1%20}
     * {%20"position":1,%20"target_url":"/activity/carInsurance",%20"source":1,%20"model":1,%20"type":2%20}
     * @link: http://www.activity.com/wechat/api3.0/Volume?data={%20%22position%22:1,%20%22target_url%22:%22/activity/carInsurance%22,%20%22source%22:1,%20%22model%22:1,%20%22type%22:2%20}
     */
    public function Volume(){
        $data = Input::get("data");//获取参数
        //判断获取的参数是否是数组格式，如果不是转成数组
        if(!is_array($data)){
            $data = json_decode($data, true);
        }
        if($data['type'] == 1){
            //如果type==1,表明请求来自于banner，后端会操作不用的数据库

            if(array_key_exists('banner_id',$data) && $data['banner_id']){
                $banner_id = $data['banner_id'];
                if(array_key_exists('target_url',$data) && $data['target_url']){
                    $target_url = substr( $data['target_url'], 0, 1 );
                    if($target_url == '/'){
                        $data['target_url'] = substr( $data['target_url'], 1 );
                    }
                    $url = Config::get("config.browse_url").$data["target_url"]."?source=".$data["source"]."&model=".$data["model"]."&banner_id=".$data['banner_id'];
                }else{
                    return Response::json(array("code" => 200011, "msg" =>'跳转路径target_url必传'));
                }
            }else{
                return Response::json(array("code" => 200010, "msg" =>'轮播图id必传'));
            }

            if(array_key_exists('source',$data) && $data['source']){
                $source = $data['source'];
            }else{
                $source = 1;
            }

            if(array_key_exists('model',$data) && $data['model']){
                $model = $data['model'];
            }else{
                $model = 1;
            }
            if(array_key_exists('identification',$data)){
                $identification = $data['identification'];
            }else{
                $identification = '';
            }
            $banner = new BannerBrowse();
            $banner->url = $url;
            $banner->source = $source;
            $banner->model = $model;
            $banner->identification = $identification;
            $banner->banner_id = $banner_id;
            $banner->created_at = date("Y-m-d H:i:s",time());
            $banner->updated_at = date("Y-m-d H:i:s",time());
            $banner->save();

            //同时修改轮播表
            BannerRecords::where("id",$banner_id)->increment('browse',1);
            Log::info("info", array("system" => "api3.0" ,"event" => "wechat/api3.0/Volume",  'input' =>Input::all() ,"output" => Response::json(array("code" => 0, "msg" =>'ok'))));
            return Response::json(array("code" => 0, "msg" =>'ok'));
        }elseif($data['type'] == 2){
            //请求来自于当季推荐
            if(array_key_exists('position',$data) && $data['position']){
                $position = $data['position'];
                if(array_key_exists('target_url',$data) && $data['target_url']){
                    $target_url = substr( $data['target_url'], 0, 1 );
                    if($target_url == '/'){
                        $data['target_url'] = substr( $data['target_url'], 1 );
                    }
                    $url = Config::get("config.browse_url").$data["target_url"]."?source=".$data["source"]."&model=".$data["model"]."&position=".$data['position'];
                }else{
                    return Response::json(array("code" => 200011, "msg" =>'跳转路径target_url必传'));
                }
            }else{
                return Response::json(array("code" => 200010, "msg" =>'position必传'));
            }

            if(array_key_exists('source',$data) && $data['source']){
                $source = $data['source'];
            }else{
                $source = 1;
            }

            if(array_key_exists('model',$data) && $data['model']){
                $model = $data['model'];
            }else{
                $model = 1;
            }
            if(array_key_exists('identification',$data)){
                $identification = $data['identification'];
            }else{
                $identification = '';
            }

            $banner = new RecommendedSeasonBrowse();
            $banner->url = $url;
            $banner->source = $source;
            $banner->model = $model;
            $banner->identification = $identification;
            $banner->position = $data['position'];
            $banner->created_at = date("Y-m-d H:i:s",time());
            $banner->updated_at = date("Y-m-d H:i:s",time());
            $banner->save();
            Log::info("info", array("system" => "api3.0" ,"event" => "wechat/api3.0/Volume",  'input' =>Input::all() ,"output" => Response::json(array("code" => 0, "msg" =>'ok'))));
            return Response::json(array("code" => 0, "msg" =>'ok'));
        }else{
            return Response::json(array("code" => 200013, "msg" =>'缺少参数'));

        }

    }

}